//
//  HWCustomSheet.m
//  HuaWan
//
//  Created by HuaWan  on 2022.
//  Copyright © 2022 HuaWan . All rights reserved.
//

#import "HWCustomSheet.h"

@implementation HWCustomSheet

- (void)awakeFromNib {
    [super awakeFromNib];
    // Do any additional setup after loading the view.
    _textField.delegate = self;
    
    [_myButtons enumerateObjectsUsingBlock:^(UIButton *b, NSUInteger idx, BOOL * _Nonnull stop) {
        if (b.tag == 10) {
            b.layer.borderWidth = 1.0;
        } else {
            b.layer.borderWidth = 0.0;
        }
        b.layer.borderColor = [UIColor grayColor].CGColor;
    }];
}

- (void)setData:(id)data delegate:(id)delegate {
    _delegate = delegate;
    
    if ([data isKindOfClass:[HWCustomSheetModel class]]) {
        HWCustomSheetModel *model = data;
        _model = model;

        _textField.text = model.text;
    }
}

#pragma mark  FKDJoinAtlasCellDelegat 取消
    
- (IBAction)buttonAction:(UIButton *)sender {
    if (_delegate && [_delegate respondsToSelector:@selector(customSheet:buttonAction:)]) {
        [_delegate customSheet:self buttonAction:sender];
    }
}

- (IBAction)textFieldChange:(UITextField *)sender {
    if (_delegate && [_delegate respondsToSelector:@selector(customSheet:textField:)]) {
        [_delegate customSheet:self textField:sender];
    }
}

- (IBAction)textFieldBegin:(UITextField *)sender {
    if (_delegate && [_delegate respondsToSelector:@selector(customSheet:textField:)]) {
        [_delegate customSheet:self textField:sender];
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    //判断是不是“删除”字符
    if (string.length != 0) {//不是“删除”字符
        NSString *totalString = [NSString stringWithFormat:@"%@%@", textField.text,string];
        if (totalString.length > 20) {
            return NO;
        }
    }
    return YES;
}

@end



#pragma mark - Model

@implementation HWCustomSheetModel
    
+ (HWCustomSheetModel *)ittemModelWithText:(NSString *)text {
    HWCustomSheetModel *model = [HWCustomSheetModel new];
    model.text = kSafeString(text);
    return model;
}
    
@end
